package com.qn.example.controller;
import com.qn.example.pojo.*;
import com.qn.example.pojo.dto.DishDto;
import com.qn.example.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author 21276
 * @version 1.0
 * @date 2023/5/5 10:03
 */
@RestController
@Slf4j
@RequestMapping("/dish")
@CrossOrigin
public class dishController {

    @Resource
    private DishService dishService;

    /**
     * 分页
     */
    @GetMapping("/page")
    public R page(@RequestParam(value = "page",defaultValue = "1") Integer page,
                  @RequestParam(value = "pageSize",defaultValue = "5") Integer pageSize,String name){
        //调用业务层的获取每一页对象的方法

        Page pageBean = dishService.findByPage(page,pageSize,name);
        return  R.success(pageBean);
    }

    /**
     * 作用:保存菜品数据
     * @return
     */
    @PostMapping
    public R save(@RequestBody DishDto dishDto, HttpSession session){
        //1. 补全创建人与修改人
        Long empId = (Long) session.getAttribute("user");
        dishDto.setCreateUser(empId);
        dishDto.setUpdateUser(empId);
        //2.把数据交给service
        dishService.save(dishDto);
        return R.success("添加成功");
    }

    /**
     * 请求路径：  http://localhost:8080/dish/1397850140982161409
     * 请求方式:  GET
     * 根据id查询当前菜品信息和对应的口味信息，用于修改页面中菜品信息回显。
     */
    @GetMapping("/{id}")
    public R findById(@PathVariable Long id){
        DishDto dishDto = dishService.findById(id);
        return R.success(dishDto);
    }


    /**
     * 请求路径：  http://localhost:8080/dish/
     * 请求方式:  PUT
     * 作用：修改菜品
     */
    @PutMapping
    public R updateDish(@RequestBody  DishDto dishDto,HttpSession session){
        //1. 补全创建人与修改人
        Long empId = (Long) session.getAttribute("user");
        dishDto.setUpdateUser(empId);
        //2. 交给service
        dishService.updateDish(dishDto);
        return R.success("修改菜品成功");
    }


    //批量删除
    @DeleteMapping
    public R delete(@RequestParam("ids") List<Long> ids){
        System.out.println("删除的id："+ids);
        dishService.delete(ids);
        return R.success("删除成功");
    }

    /**
     * 批量修改菜品状态
     * @PathVariable Long id
     */
    @PostMapping("/status/{status}")
    public R dishStatusByStatus(@PathVariable("status") Integer status,@RequestParam("ids") List<Long> ids){
        System.err.print("修改菜品状态：" + status);

        System.err.print("修改菜品id：");
        ids.forEach(System.err::println);


        //2. 交给service
        dishService.dishStatusByStatus(status,ids);
        return R.success("修改菜品状态成功");
    }



    @GetMapping("/list")
    public  R  list(Long categoryId,Integer status){
        //1. 调用service方法得到菜品列表
        List<DishDto> dishDtoList = dishService.list(categoryId,status);
        return R.success(dishDtoList);
    }

}
